<?php

class AppController extends Controller {
	var $persistModel = true;
	var $components = array('Acl', 'Auth','RequestHandler','Email');
	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->authorize = 'actions';
		$this->Auth->userModel = 'User';
		if($this->RequestHandler->isAjax()){
			Configure::write('debug', 0);
			$this->layout = 'json';
			$this->view="Json";
			$this->Auth->autoRedirect = false;
		}else{
			if(isset($this->params['prefix'])&&$this->params['prefix']=="admin"){
				$this->layout="scaffold";
			}else{
				$this->layout="default";
			}
			$this->Auth->loginAction = array('admin' => false,'controller' => 'launcher','action' => 'index');
			$this->Auth->loginRedirect = array('admin' => false,'controller' => 'launcher', 'action' => 'index');
		}
		$this->Auth->fields = array('username' => 'username','password' => 'password');
		$this->Auth->userScope = array('User.banned' => 0,'User.activated'=>1);
	}
	function beforeRender(){
		if($this->RequestHandler->isAjax()){
			if ($this->Acl->check($this->Auth->user(),$this->Auth->action(),$this->Auth->allowedActions)) {
				$this->set('user',array("success"=>0,"err"=>"Not Logged in.","errcode"=>1));
				$this->set('json',array("user"));
				$this->render();
			}
		}
	}
	protected function _get_real_ip(){
		$ip=false;
		if(!empty($_SERVER["HTTP_CLIENT_IP"])){
			$ip=$_SERVER["HTTP_CLIENT_IP"];
		}
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ips=explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
			if ($ip) { array_unshift($ips, $ip); $ip=FALSE; }
			for ($i=0; $i < count($ips); $i++) {
				if (!eregi ("^(10|172.16|192.168).", $ips[$i])) {
					$ip=$ips[$i];
					break;
				}
			}
		}
		return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
	}
};

?>
